﻿@using HCMS.Extensions
@using HCMS.Dictionary;
@model HCMS.Entities.Manage.Menu
@{
    Layout = "~/Views/Shared/_Layout_Edit_Pear_Layui.cshtml";

    var dictTheme = new Dictionary<string, string>() {
                        { "layui-btn","默认按钮"},
                        { "layui-btn layui-bg-blue","蓝色按钮"},
                        { "layui-btn layui-bg-orange","橙色按钮"},
                        { "layui-btn layui-bg-red","红色按钮"},
                        { "layui-btn layui-bg-purple","紫色按钮"},
                        { "layui-btn layui-btn-disabled","禁用按钮"},

                        { "layui-btn layui-btn-primary layui-border-green","主色按钮"},
                        { "layui-btn layui-btn-primary layui-border-blue","蓝色按钮"},
                        { "layui-btn layui-btn-primary layui-border-orange","橙色按钮"},
                        { "layui-btn layui-btn-primary layui-border-red","红色按钮"},
                        { "layui-btn layui-btn-primary layui-border-purple","紫色按钮"},
                        { "layui-btn layui-btn-primary layui-border","普通按钮"}
                    };

    var dictSize = new Dictionary<string, string>() {
                        { "layui-btn layui-btn-lg","大型按钮"},
                        { "layui-btn","默认按钮"},
                        { "layui-btn layui-btn-sm","小型按钮"},
                        { "layui-btn layui-btn-xs","迷你按钮"},

                        { "layui-btn layui-btn-lg layui-btn-normal","大型按钮"},
                        { "layui-btn layui-btn-normal","默认按钮"},
                        { "layui-btn layui-btn-sm layui-btn-normal","小型按钮"},
                        { "layui-btn layui-btn-xs layui-btn-normal","迷你按钮"},

                        { "layui-btn layui-btn-primary layui-btn-lg","大型按钮"},
                        { "layui-btn layui-btn-primary","默认按钮"},
                        { "layui-btn layui-btn-primary layui-btn-sm","小型按钮"},
                        { "layui-btn layui-btn-primary layui-btn-xs","迷你按钮"},
                    };

    var dictRadius = new Dictionary<string, string>() {
                        { "layui-btn","默认 直角"},
                        { "layui-btn layui-btn-primary layui-btn-radius","原始按钮"},
                        { "layui-btn layui-btn-radius","默认按钮"},
                        { "layui-btn layui-btn-normal layui-btn-radius","百搭按钮"},
                        { "layui-btn layui-btn-warm layui-btn-radius","暖色按钮"},
                        { "layui-btn layui-btn-danger layui-btn-radius","警告按钮"},
                        { "layui-btn layui-btn-disabled layui-btn-radius","禁用按钮"}
                    };
}

<div class="layui-body layui-bg-gray" style="left:0;">
    <div class="layui-card">
        <form class="layui-form">
            <input type="hidden" asp-for="Id" />

            <div class="layui-tab layui-tab-card">
                <ul class="layui-tab-title">
                    <li class="layui-this">基础信息</li>
                    <li class="js-tab-set-style">按钮样式</li>
                    <li>其它</li>
                </ul>

                <div class="layui-tab-content">
                    <!-- 基础信息 -->
                    <div class="layui-tab-item layui-show">

                        <div class="layui-form-item">
                            <div class="layui-form-label">名称</div>
                            <div class="layui-input-inline" style="width:140px;">
                                <input type="text" asp-for="Name" lay-verify="required" autocomplete="off" placeholder="请填写名称"
                                       class="layui-input" />
                            </div>

                            <label class="layui-form-label">图标</label>
                            <div class="layui-input-inline" style="width:350px;">
                                <input type="text" name="Icon" id="IconPicker" value="@Model.Icon"
                                       class="layui-input" />
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-form-label">显示名称</div>
                            <div class="layui-input-inline" style="width:140px;">
                                <input type="text" asp-for="DisplayName" lay-verify="required" autocomplete="off" placeholder="请填写名称"
                                       class="layui-input" />
                            </div>

                            <div class="layui-form-label">上级菜单</div>
                            <div class="layui-input-inline" style="width:200px;">
                                <div id="classTree"></div>
                                <input type="hidden" asp-for="ParentId" />
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <div class="layui-form-label">类型</div>
                            <div class="layui-input-inline" style="width:140px;">
                                <select asp-for="TypeId" asp-items="new DictCommon().MenuTypeItems()" lay-filter="type-select-filter"></select>
                            </div>
                        </div>

                        <div class="layui-form-item js-controller-box">
                            <div class="layui-form-label">控制器名称</div>
                            <div class="layui-input-inline" style="width:180px;">
                                <input type="text" asp-for="ControllerName" autocomplete="off" placeholder="请填写控制器名称"
                                       class="layui-input" />
                            </div>
                        </div>
                        <div class="layui-form-item js-action-box">
                            <div class="layui-form-label">方法名称</div>
                            <div class="layui-input-inline" style="width:180px;">
                                <input type="text" asp-for="ActionName" autocomplete="off" placeholder="请填写方法名称"
                                       class="layui-input" />
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline" style="width:140px;">
                                <select asp-for="Status" asp-items="new DictCommon().StatusItems()"></select>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">排序</label>
                            <div class="layui-input-inline" style="width:80px;">
                                <input type="number" asp-for="Orders" lay-affix="number" min="1"
                                       class="layui-input" />
                            </div>
                        </div>
                    </div>

                    <!-- 按钮样式-->
                    <div class="layui-tab-item js-style-box">
                        <div class="layui-form-item">
                            <div class="layui-form-label">主题</div>
                            <div class="layui-input-inline" style="width:83%;">
                                @{
                                    styleItemTemplate("theme", dictTheme, 6);
                                }
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-form-label">尺寸</div>
                            <div class="layui-input-inline" style="width:83%;">
                                @{
                                    styleItemTemplate("size", dictSize, 4);
                                }
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-form-label">圆角</div>
                            <div class="layui-input-inline" style="width:83%;">
                                @{
                                    styleItemTemplate("radius", dictRadius, 4);
                                }
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-form-label">预览效果</div>
                            <div class="layui-input-inline" style="width:180px;">
                                <button type="button" id="js-btn-style-preview" class="@(Model.Style.ToString("layui-btn"))">默认按钮</button>
                                <input type="hidden" name="Style" id="Style" value="@(Model.Style.ToString("layui-btn"))" />
                            </div>
                        </div>
                    </div>

                    <!-- 其它 -->
                    <div class="layui-tab-item">
                        @if (Model.Id > 0)
                        {
                            <div class="layui-form-item">
                                <div class="layui-form-label">创建时间</div>
                                <div class="layui-form-label content-label">
                                    @Model.CreateTime @Model.CreateUser
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-form-label">更新时间</div>
                                <div class="layui-form-label content-label">
                                    @Model.UpdateTime @Model.UpdateUser
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-fixbar btn-fixbar-box">
                <div class="layui-input-block">
                    <vc:menu-submit action-names="@(new string[]{"save"})"></vc:menu-submit>
                    <button type="button" class="layui-btn layui-btn-primary layui-border-red" lay-on="close">
                        <i class="layui-icon layui-icon-close"></i>关闭
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

@functions {
    @* 样式 - 项目模板 公共方法 *@
    private void styleItemTemplate(string type, Dictionary<string, string> dict, int lineIndex)
    {
        var i = 0;
        foreach (var item in dict)
        {
            // 换行处理
            var lineStr = "";
            if (i > 0 && lineIndex > 0 && (i + 1) % lineIndex == 0)
            {
                lineStr = "<br />";
            }

            bool isChecked = false;
            // 默认选中第一项
            if (item.Key == "layui-btn" && (string.IsNullOrWhiteSpace(Model.Style) || Model.Style == "layui-btn"
            || dict.Count(t => Model.Style.Replace("layui-btn ", "").Contains(t.Key.Replace("layui-btn ", ""))) > 0))
            {
                isChecked = true;
            }
            // 选中其它项
            else if (item.Key != "layui-btn" && Model.Style.ToString("").Contains(item.Key.Replace("layui-btn ", "")))
            {
                isChecked = true;
            }

            <input type="radio" name="style-@(type)" value="@item.Key" title="@item.Value" lay-filter="style-@(type)-filter" @(isChecked ? "checked=checked" : "")>
            <div lay-radio>
                <button type="button" class="@item.Key">@item.Value</button>
            </div>
            @(Html.Raw(lineStr))

            i++;
        }
    }
}

@section Scripts {
    <script>
        layui.use(['form', 'jquery', 'iconPicker', 'xmSelect'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            let layer = layui.layer;
            let iconPicker = layui.iconPicker;

            typeSelect();
            iconSelect();
            createTree();
            styleCustom();
            submitFn();

            // 类型选择事件
            function typeSelect(){
                var $controllerBox = $('.js-controller-box');
                var $actionBox = $('.js-action-box');
                var $tabSetStyle = $('.js-tab-set-style');
                var $styleBox = $('.js-style-box');

                // 初始化
                switchHide($('#TypeId').val());

                // 选择事件
                form.on('select(type-select-filter)',function(data){
                    var value = data.value;
                    switchHide(value);
                });

                // 切换显示/隐藏 非必填功能
                function switchHide(typeId){
                    if(parseInt(typeId) === 1){
                        $controllerBox.addClass('layui-hide');
                        $actionBox.addClass('layui-hide');
                    }else{
                        $controllerBox.removeClass('layui-hide');
                        $actionBox.removeClass('layui-hide');
                    }

                    if(parseInt(typeId) === 3){
                        $tabSetStyle.removeClass('layui-hide');
                        $styleBox.removeClass('layui-hide');
                    } else {
                        $tabSetStyle.addClass('layui-hide');
                        $styleBox.addClass('layui-hide');
                    }
                }
            }

            // 图标选择器
            function iconSelect() {
                iconPicker.render({
                    elem: '#IconPicker',
                    type: 'fontClass',
                    svgUrl: '/pear/component/layui/font/iconfont.svg',
                    cellWidth: '28%'
                });
            }

            // 创建分类树
            function createTree() {
                $.ajax({
                    url: '/menu/alltree/',
                    type: 'get',
                    success: function (result) {
                        successFn(result);
                    }
                });

                function successFn(data) {
                    var $ParentId = $('#ParentId');

                    xmSelect.render({
                        el: '#classTree',
                        radio: true,
                        height: '300px',
                        initValue: [$ParentId.val()],    // 初始化选中值
                        prop: {
                            name: 'name',
                            value: 'id'
                        },
                        data: data,
                        tree: {
                            // 是否显示树状结构
                            show: true,
                            // 是否严格遵守父子模式
                            strict: false,
                            // 是否开启极简模式
                            simple: false
                        },
                        model: {
                            label: {
                                block: {
                                    showIcon: false // 隐藏删除按钮
                                }
                            }
                        },
                        // 选中事件
                        on: function (data) {
                            if (data.isAdd) {
                                $ParentId.val(data.arr[0].id);
                            }
                        }
                    });
                }
            }

            // 样式自定义
            function styleCustom() {

                // 主题
                form.on('radio(style-theme-filter)', function (data) {
                    setValue();
                });

                // 尺寸
                form.on('radio(style-size-filter)', function (data) {
                    setValue();
                });

                // 圆角
                form.on('radio(style-radius-filter)', function (data) {
                    setValue();
                });

                // 设置选中值
                var $btnStylePreview = $('#js-btn-style-preview');
                var $style = $('#Style');
                function setValue() {
                    var theme = $('input[name="style-theme"]:checked').val() || '';
                    var size = $('input[name="style-size"]:checked').val() || '';
                    var radius = $('input[name="style-radius"]:checked').val() || '';

                    var result = [];
                    result.push('layui-btn');
                    result.push(theme.replace('layui-btn', ''));
                    result.push(size.replace('layui-btn', ''));
                    result.push(radius.replace('layui-btn', ''));

                    var style = result.join(' ').replace(/\s+/g, ' ');
                    style = $.trim(style);
                    $btnStylePreview.attr('class', style);
                    $style.val(style);
                }
            }

            // 表单提交
            function submitFn() {
                form.on('submit(save)', function (data) {

                    var layerIndex = layer.msg('正在保存', {
                      icon: 16,
                      shade: 0.1,
                      time: 0
                    });

                    $.ajax({
                        url: '/menu/save/',
                        type: 'post',
                        dataType: 'json',
                        data: data.field,
                        success: function (result) {
                            if (result.code === 200) {
                                layer.msg(result.message, { icon: 1, time: 1000 }, function () {
                                    parent.layer.close(parent.layer.getFrameIndex(window.name)); // 关闭当前页
                                    parent.layui.table.reload("tableList", {
                                        page: { curr: 1 },
                                    });
                                });
                            } else {
                                parent.layer.msg(result.message, { icon: 2 })
                            }
                        },
                        complete‌: function(){
                            layer.close(layerIndex);
                        }
                    });

                    return false;
                });
            }
        });
    </script>
}